package cn.hlife.com.hlfhaemocyte.measure.view;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.greendaodemo.greendao.gen.DaoMaster;
import com.greendaodemo.greendao.gen.DaoSession;
import com.greendaodemo.greendao.gen.MeasureEntityDao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import cn.hlife.com.hlfhaemocyte.R;
import cn.hlife.com.hlfhaemocyte.bean.MeasureEntity;

public class MeasureRecordActivity extends AppCompatActivity implements View.OnClickListener{
    ListView  listView;
    MeasureAdapter  measureAdapter;
//    private DaoSession daoSession;
    private ImageView returnImage;
    private  TextView editText;
    private LinearLayout operationLayout;
    private  Button selectAll,delete,cancle;
    MeasureEntityDao measureEntityDao;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_measure_record2);
        listView=(ListView)findViewById(R.id.listView);
        selectAll=(Button)findViewById(R.id.select_all);
        delete=(Button)findViewById(R.id.delete);
        cancle=(Button)findViewById(R.id.cancle);
        returnImage=(ImageView)findViewById(R.id.return_image);
        operationLayout=(LinearLayout)findViewById(R.id.operation_layout);
        editText=(TextView)findViewById(R.id.edit_text);
        returnImage.setOnClickListener(this);
        measureAdapter =new MeasureAdapter();
        listView.setAdapter(measureAdapter);
        //创建dbmanager类初始化数据库
        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(this.getApplicationContext(), "user.db", null);
        DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDb());
        DaoSession daoSession= daoMaster.newSession();
        measureEntityDao =  daoSession.getMeasureEntityDao();
        List<MeasureEntity> listAll= measureEntityDao.queryBuilder()
                .orderDesc(MeasureEntityDao.Properties.Id)
                .list();
        measureAdapter.setList(listAll);
        editText.setOnClickListener(this);
        cancle.setOnClickListener(this);
        delete.setOnClickListener(this);
        selectAll.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
      int  id=  v.getId();
        switch (id){
            case  R.id.return_image:
                MeasureRecordActivity.this.finish();
                break;
            case R.id.edit_text:
                measureAdapter.setMode(MeasureAdapter.DeleteMode);
                operationLayout.setVisibility(View.VISIBLE);
                editText.setVisibility(View.GONE);
                break;
            case R.id.cancle:
                measureAdapter.cleanSelectState();
                measureAdapter.setMode(MeasureAdapter.DefaultMode);
                operationLayout.setVisibility(View.GONE);
                editText.setVisibility(View.VISIBLE);
                break;
            case R.id.select_all:
                measureAdapter.setSelectAll();
                break;
            case R.id.delete:
                showAlertDialog();

                break;

        }
    }

    class    MeasureAdapter  extends BaseAdapter{

        HashMap<Integer,Boolean>   checkList;
        List<MeasureEntity> listAll=new ArrayList<MeasureEntity>();
       public static final int DefaultMode=101,DeleteMode=102;
        int ShowMode=100;
        public  MeasureAdapter(){
            ShowMode=DefaultMode;
            checkList=new HashMap<Integer,Boolean>();
        }
        public  void  setList(List<MeasureEntity> listAll){
            this.listAll.clear();
            this.listAll.addAll(listAll);
            checkList.clear();
            for(int i=0;i<listAll.size();i++){
                checkList.put(i,false);
            }
            notifyDataSetChanged();
        }
        public void  setMode(int  mode){
            this.ShowMode=mode;
            notifyDataSetChanged();

        }
        public  void setSelectAll(){
            for(int i=0;i<listAll.size();i++){
                checkList.put(i,true);
            }
            notifyDataSetChanged();
        }
        public  void  cleanSelectState(){
            for(int i=0;i<listAll.size();i++){
                checkList.put(i,false);
            }
        }
        public  void  delete(){
           List<Long> listAllTemp=new ArrayList<Long>();
           Set<Integer> keySet=checkList.keySet();
           Iterator    iterator= keySet.iterator();
            while (iterator.hasNext()){
               int  position=(int) iterator.next();
                boolean  value=checkList.get(position);
                if(value){
                   long  id=  listAll.get(position).getId();
                    listAllTemp.add(id);
                }
            }
            measureEntityDao.deleteByKeyInTx(listAllTemp);
            List<MeasureEntity> listAll= measureEntityDao.queryBuilder()
                    .orderDesc(MeasureEntityDao.Properties.Id)
                    .list();
            setList(listAll);
        }
        @Override
        public int getCount() {
            return listAll.size();
        }

        @Override
        public Object getItem(int position) {
            return listAll.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View   view=null;
            view  =convertView;
            final ViewHolder  viewHolder;
            if (view==null) {
                viewHolder=new ViewHolder();
                view =LayoutInflater.from(MeasureRecordActivity.this).inflate(R.layout.measure_record_item,parent,false);
                viewHolder.nameText = (TextView) view.findViewById(R.id.name_text);
                viewHolder.timeText = (TextView) view.findViewById(R.id.time_text);
                viewHolder.caseText = (TextView) view.findViewById(R.id.case_number);
                viewHolder.intoImage = (ImageView) view.findViewById(R.id.into_image);
                viewHolder.checkBox = (CheckBox) view.findViewById(R.id.checkBox);
                view.setTag(viewHolder);
            }else{
                viewHolder=(ViewHolder) view.getTag();
            }
            final MeasureEntity   measureEntity= listAll.get(position);
            viewHolder.nameText.setText(measureEntity.getMUserName());
            viewHolder.timeText.setText(measureEntity.getMeasueDate());
            viewHolder.caseText.setText(measureEntity.getMMedicalNumber());
            if(ShowMode==DefaultMode){
                viewHolder.intoImage.setVisibility(View.VISIBLE);
                viewHolder.checkBox.setVisibility(View.GONE);
            }else if(ShowMode==DeleteMode){
                boolean   isSelect=checkList.get(position);
                viewHolder.checkBox.setChecked(isSelect);
                viewHolder.intoImage.setVisibility(View.GONE);
                viewHolder.checkBox.setVisibility(View.VISIBLE);
            }
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(ShowMode==DefaultMode){
                        Intent  intent=new Intent(MeasureRecordActivity.this,MeasureRecordDetailsActivity.class);
                        intent.putExtra("_id",measureEntity.getId());
                        intent.putExtra("isEdit",false);
                        startActivity(intent);
                    }else  if(ShowMode==DeleteMode){
                        boolean   isSelect=checkList.get(position);
                        viewHolder.checkBox.setChecked(!isSelect);
                        checkList.put(position,!isSelect);
                        notifyDataSetChanged();
                    }
                }
            });
            return view;
        }
        class   ViewHolder {
            private TextView  nameText,timeText,caseText;
            private  ImageView  intoImage;
            private CheckBox checkBox;
        }
    }
    public  void  showAlertDialog(){
        AlertDialog.Builder  builder=new  AlertDialog.Builder(MeasureRecordActivity.this);
        builder.setTitle("系统提示");
        builder.setMessage("是否删除选中的条目？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                measureAdapter.delete();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.show();
    }
}
